﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JWFW.SFL
{
    public class HexOP
    {
        /// <summary>
        /// 将数字由第一个进制转换成第二个进制
        /// </summary>
        /// <param name="inputNumber"></param>
        /// <param name="fromBase"></param>
        /// <param name="toBase"></param>
        /// <returns></returns>
        public static string ConvertIntBase(string inputNumber, int fromBase, int toBase)
        {
            int intValue = Convert.ToInt32(inputNumber, fromBase);
            return Convert.ToString(intValue, toBase);
        }
        /// <summary>
        /// 截断二进制数组
        /// </summary>
        /// <param name="input"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static byte[] TruncateBytes(byte[] input, int length)
        {
            byte[] res = new byte[length];
            for (int i = 0; i < length; i++)
            {
                res[i] = input[i];
            }
            return res;
        }
        /// <summary>
        /// 连接两个Byte数组
        /// </summary>
        /// <param name="a">第一个Byte数组</param>
        /// <param name="b">第二个Byte数组</param>
        /// <returns></returns>
        public static byte[] CombineBytes(byte[] a, byte[] b)
        {
            byte[] res = new byte[a.Length + b.Length];
            a.CopyTo(res, 0);
            b.CopyTo(res, a.Length);
            return res;
        }
        /// <summary>
        /// 剔除16进制数据中的空格回车等，用于转换成字符串
        /// </summary>
        /// <param name="hexData"></param>
        /// <returns></returns>
        public static string RefineHex(string hexData)
        {
            return hexData.Replace(" ", "").Replace("\r", "").Replace("\n", "");
        }
        /// <summary>
        /// 将16进制的数据字符串转换成byte数组
        /// </summary>
        /// <param name="hexData"></param>
        /// <returns></returns>
        public static byte[] HexToBytes(string hexData)
        {
            hexData = RefineHex(hexData);
            int length = hexData.Length / 2;
            byte[] res = new byte[length];
            for (int i = 0; i < length; i++)
            {
                string tmp = hexData.Substring(i * 2, 2);
                res[i] = Convert.ToByte(tmp, 16);
            }
            return res;
        }
        /// <summary>
        /// 将byte数组转换成16进制的字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string BytesToHex(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder(100);
            for (int i = 0; i < bytes.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(" ");
                }
                sb.Append(Convert.ToString((int)bytes[i], 16).PadLeft(2, '0'));
            }
            return sb.ToString().Trim();
        }
        /// <summary>
        /// 16进制还原成字符串
        /// </summary>
        /// <param name="hexData"></param>
        /// <param name="zeroNum"></param>
        /// <returns></returns>
        public static string HexToASCIIString(string hexData, int zeroNum = 0)
        {
            StringBuilder sb = new StringBuilder(100);
            hexData = RefineHex(hexData);
            for (int i = 0; i < hexData.Length; i++)
            {
                if (i % 2 == 0)
                {
                    string tmp = hexData.Substring(i, 2);
                    if (tmp == "00")
                    {
                        sb.Append((char)zeroNum);
                    }
                    else
                    {
                        sb.Append((char)Convert.ToInt32(tmp, 16));
                    }
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 字符串转换成16进制的字符串，每个字节间包含空格
        /// </summary>
        /// <param name="input"></param>
        /// <param name="zeroNum"></param>
        /// <returns></returns>
        public static string ASCIIStringToHex(string input, int zeroNum = 0)
        {
            StringBuilder sb = new StringBuilder(100);
            for (int i = 0; i < input.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(" ");
                }
                if ((int)input[i] == zeroNum)
                {
                    sb.Append("00");
                }
                else
                {
                    sb.Append(Convert.ToString((int)input[i], 16).PadLeft(2, '0'));
                }
            }
            return sb.ToString().Trim();
        }
    }
}
